package com.ly.hotel.spainterface.jxd.dialog;

import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.spainterface.jg.view.CustomToast;
import com.ly.hotel.spainterface.jxd.adapter.JxdJSItemAdapter;
import com.ly.hotel.spainterface.jxd.bean.MachineBean;
import com.ly.hotel.spainterface.jxd.bean.Massage;
import com.ly.hotel.spainterface.jxd.bean.PluginBean;
import com.ly.hotel.spainterface.jxd.bean.Therapy;
import com.ly.hotel.spainterface.jxd.event.JxdCloseJSDetailDialogEvent;
import com.ly.hotel.spainterface.jxd.utils.AvoidDoubleClickedUtils;
import com.ly.hotel.util.ImageUtil;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by ThinkPad on 2018/3/23.
 */

public class JxdJSDetailDialog extends DialogFragment {

    private final String TAG = "JxdJSActivity";
    private Context context;
    private String artNo;
    private int rod;
    private TextView therapyNo;
    private TextView therapyName;
    private TextView therapyStatus;
    private TextView therapySex;
    private TextView therapyHome;
    private TextView therapyHeight;
    private GridView itemSelectGridViw;
    private Button therapyArrange;
    private ImageView therapyImage;
    private ImageButton therapyDialogClose;
    private TextView therapyDowntime;
    private TextView therapyAge;
    private Therapy therapy;
    private PluginBean pluginBean;
    private MachineBean machineBean;
    private String isHall;
    private TextView noHallCannotArrange;
    private String isSpec = "";
    private String arrangeTimeOut;
    private List<Therapy> therapies;
    private View view;
    private Massage massage;
    private String massageName;// 推荐技师过来的做钟名称
    private int selectPosion = 0;// 推荐技师时，选的项目


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = MyApplication.getContext();
        rod = getResources().getDimensionPixelSize(R.dimen.px6);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    // 安排技师成功后，关闭技师详情窗口
    public void onEventMainThread(JxdCloseJSDetailDialogEvent detailDialogEvent){
        dismiss();
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            // 设置Dialog宽度占比为60%，高度为wrap_content
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.64), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        /*Window window = dialog.getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.windowAnimations = R.style.customDialogAnim;
        window.setAttributes(layoutParams);*/
    }

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        view = inflater.inflate(R.layout.jxd_therapy_details_layout,container,false);
        initView();

        setTherapyData();
        setSelectItem();
        setArrange();
        return view;
    }

    public void initView(){

        therapyNo = view.findViewById(R.id.therapy_no);
        therapyName = view.findViewById(R.id.therapy_name);
        therapyStatus = view.findViewById(R.id.therapy_status);
        therapyDowntime = view.findViewById(R.id.therapy_downtime);
        therapySex = view.findViewById(R.id.therapy_sex);
        therapyHome = view.findViewById(R.id.therapy_home);
        therapyHeight = view.findViewById(R.id.therapy_height);
        therapyAge = view.findViewById(R.id.therapy_age);
        itemSelectGridViw = view.findViewById(R.id.therapy_item_gridview);
        therapyArrange = view.findViewById(R.id.therapy_arrange);
        therapyImage = view.findViewById(R.id.details_therapy_iamge);
        therapyDialogClose = view.findViewById(R.id.therapy_dialog_close);
        noHallCannotArrange = view.findViewById(R.id.no_hall_cannot_arrange);

        therapyDialogClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        // 全身项目隐藏安排按钮
        if(isHall.equals("否")){
            therapyArrange.setVisibility(View.GONE);
            noHallCannotArrange.setText(getResources().getString(R.string.can_not_arrange_tips));
            noHallCannotArrange.setVisibility(View.VISIBLE);
        }

        // 根据配置，决定是否只能浏览，不能安排
        if(pluginBean != null){
            if(pluginBean.isWaiterId()){
                therapyArrange.setVisibility(View.GONE);
                noHallCannotArrange.setText(getResources().getString(R.string.only_see_tips));
                noHallCannotArrange.setVisibility(View.VISIBLE);
            }
        }
    }

    // 立即安排
    public void setArrange(){
        therapyArrange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(therapy != null){
                    if(therapy.getStatus().equals("空闲") && therapy.getOnduty().equals("当值")){
                        if(AvoidDoubleClickedUtils.isNotDoubleClicked()){
                            return;
                        }
                        JxdJSArrangeDialog arrangeDialog = new JxdJSArrangeDialog();
                        arrangeDialog.setPluginBean(pluginBean);
                        arrangeDialog.setMassageName(massage.getMassagename());
                        arrangeDialog.setMachineBean(machineBean);
                        arrangeDialog.setIsSpec(isSpec);
                        arrangeDialog.setArtNo(therapy.getArtNo());
                        arrangeDialog.setCancelable(false);
                        arrangeDialog.show(getFragmentManager(),"JxdJSArrangeDialog");
                    }else{
                        CustomToast customToast = CustomToast.makeText(context,getResources().getString(R.string.no_free_tips),Toast.LENGTH_SHORT,false);
                        customToast.setGravity(Gravity.CENTER,0,0);
                        customToast.show();
                    }
                }
            }
        });
    }

    public void setTherapyData(){
        therapy = therapies.get(0);
        String imageUrl = "";
        // 根据imgSrc配置是否使用收银系统的技师图片
        if (pluginBean != null) {
            if (!TextUtils.isEmpty(pluginBean.getImgSrc())){
                imageUrl = pluginBean.getImgSrc() + therapy.getArtNo() + ".jpg";
            }else {
                imageUrl = MyApplication.getHostResUrl() + "/upload/interface/technician/pic/" + therapy.getArtNo() + ".jpg";
            }
        }else {
            imageUrl = MyApplication.getHostResUrl() + "/upload/interface/technician/pic/" + therapy.getArtNo() + ".jpg";
        }
        ImageUtil.displayImage_Round(context,imageUrl,therapyImage,rod);
        try{
            if(therapy.getArtNo() != null){
                therapyNo.setText(Html.fromHtml(getResources().getString(R.string.technician_id) + "<font color='#00DFFF'>" + therapy.getArtNo() + "</font>"));
            }
        }catch (Exception e){}

        try{
            if(therapy.getArtName() != null){
                if(pluginBean.isShowName()){
                    therapyName.setText(getResources().getString(R.string.technician_name) + therapy.getArtName());
                }
                if(pluginBean.isNameFiltrate()){
                    if(therapy.getArtName().contains("红牌")){
                        therapyName.setText(getResources().getString(R.string.technician_name) + "红牌");
                    }
                }
            }
        }catch (Exception e){}

        try{
            if(therapy.getStatus().equals("空闲")){
                if(therapy.getOnduty().equals("当值")){
                    therapyStatus.setText(Html.fromHtml(getResources().getString(R.string.technician_status) + "<font color='#00DFFF'>" + therapy.getStatus() + "</font>"));
                }else{
                    therapyStatus.setText(Html.fromHtml(getResources().getString(R.string.technician_status) + "<font color='#00DFFF'>" + therapy.getOnduty() + "</font>"));
                }
            }else{
                therapyStatus.setText(Html.fromHtml(getResources().getString(R.string.technician_status) + "<font color='#00DFFF'>" + therapy.getStatus() + "</font>"));
            }
        }catch (Exception e){}

        try{
            if(therapy.getDownMinute() != null){
                if(pluginBean.isShowEndTime() && therapy.getStatus().equals("在做钟")){
                    therapyDowntime.setText(getResources().getString(R.string.down_time) + therapy.getDownMinute() + getResources().getString(R.string.minute));
                }
            }
        }catch (Exception e){}

        try{
            if(therapy.getSex() != null){
                therapySex.setText(getResources().getString(R.string.sex) + therapy.getSex());
            }
        }catch (Exception e){}

        try{
            if(therapy.getAge() != null){
                therapyAge.setText(getResources().getString(R.string.age) + therapy.getAge());
            }
        }catch (Exception e){}

        try{
            if(therapy.getHeight() != null){
                therapyHeight.setText(getResources().getString(R.string.height) + therapy.getHeight());
            }
        }catch (Exception e){}

        try {
            if(therapy.getHome() != null){
                therapyHome.setText(getResources().getString(R.string.home) + therapy.getHome());
            }
        }catch (Exception e){}
    }

    // 设置可选的做钟项目
    public void setSelectItem(){
        // 存储项目信息
        final List<Massage> massages = new ArrayList<>();
        for(int i = 0;i < therapies.size();i ++){
            Therapy therapyInfo = therapies.get(i);
            // 判断推荐技师选的是哪个项目
            if(!TextUtils.isEmpty(massageName)){
                if(massageName.equals(therapyInfo.getArtModeNo())){
                    selectPosion = i;
                }
            }
            // 构造可选的做钟项目
            Massage massage = new Massage();
            massage.setMassagename(therapyInfo.getArtModeNo());
            massages.add(massage);
        }

        final JxdJSItemAdapter itemAdapter = new JxdJSItemAdapter(context,massages);
        itemSelectGridViw.setAdapter(itemAdapter);
        itemSelectGridViw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                itemAdapter.setDefaultPositon(position);
                massage = massages.get(position);
                itemAdapter.notifyDataSetChanged();
            }
        });
        itemSelectGridViw.performItemClick(itemSelectGridViw.getChildAt(0),selectPosion,itemSelectGridViw.getItemIdAtPosition(0));
    }

    public void setPluginBean(PluginBean pluginBean) {
        this.pluginBean = pluginBean;
    }

    public void setMachineBean(MachineBean machineBean) {
        this.machineBean = machineBean;
    }

    public void setIsHall(String isHall) {
        this.isHall = isHall;
    }

    public void setArtNo(String artNo) {
        this.artNo = artNo;
    }

    public void setIsSpec(String isSpec) {
        this.isSpec = isSpec;
    }

    public void setArrangeTimeOut(String arrangeTimeOut) {
        this.arrangeTimeOut = arrangeTimeOut;
    }

    public void setTherapies(List<Therapy> therapies) {
        this.therapies = therapies;
    }

    public void setMassageName(String massageName) {
        this.massageName = massageName;
    }
}
